package com.spyx.order.controller;

import com.atyx.spyx.model.order.OrderInfo;
import com.atyx.spyx.vo.order.OrderConfirmVo;
import com.atyx.spyx.vo.order.OrderSubmitVo;
import com.atyx.spyx.vo.order.OrderUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.auth.AuthContextHolder;
import com.spyx.order.service.OrderService;
import com.spyx.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order")
public class OrderApiController {

    @Autowired
    private OrderService orderService;
    //确认订单
    @GetMapping("auth/confirmOrder")
    public Result confirmOrder(){
        //1.获取用户id
        Long userId = AuthContextHolder.getUserId();
        //2.调取service层方法
        OrderConfirmVo orderConfirmVo= orderService.confirmOrder(userId);
        return Result.ok(orderConfirmVo);
    }

    //生成订单
    @PostMapping("auth/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitVo orderSubmitVo){
        //1.先获取用户Id
        Long userId = AuthContextHolder.getUserId();
        orderSubmitVo.setUserId(userId);
        //2.生成订单,返回的是订单的ID值
        Long orderId= orderService.submitOrder(orderSubmitVo);
        return Result.ok(orderId);
    }

    //订单详情
    @GetMapping("auth/getOrderInfoById/{orderId}")
    public Result getOrderInfoById(@PathVariable("orderId")Long orderId){
       OrderInfo orderInfo= orderService.getOrderInfoById(orderId);
        return Result.ok(orderInfo);
    }

    //根据orderNo,查询orderInfo的所有数据
    @GetMapping("inner/getOrderInfo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(@PathVariable("orderNo")String orderNo){
        OrderInfo orderInfo= orderService.getOrderInfoByOrderNo(orderNo);
        return orderInfo;
    }

    //根据订单编号，修改订单的支付状态
    @RequestMapping("inner/updateOrderStatus/{orderNo}")
    public void updateOrderStatus(@PathVariable("orderNo")String orderNo){
            orderService.updateOrderStatus(orderNo);
    }

    //根据订单状态查询订单的数据
    @GetMapping("auth/findUserOrderPage/{page}/{limit}")
    public Result findUserOrderPage(
            @PathVariable("page")Integer page,
            @PathVariable("limit") Integer limit,
            OrderUserQueryVo queryVo   //模糊查条件
    ){
        //1.先获取用户id,查询某个人的订单信息
        Long userId = AuthContextHolder.getUserId();
        queryVo.setUserId(userId);
        // 分页数据
        Page page1 = new Page(page,limit);
       IPage<OrderInfo> infoIPage= orderService.findUserOrderPage(page1,queryVo);

        return Result.ok(infoIPage);
    }

}
